from __future__ import annotations

import unittest
import asyncio
from unittest.mock import MagicMock
from g4f.errors import MissingRequirementsError
try:
    from g4f.gui.server.backend_api import Backend_Api
    has_requirements = True
except:
    has_requirements = False
try:
    from g4f.tools.web_search import search
    has_search = True
except:
    has_search = False
try:
    from ddgs.exceptions import DDGSException
except ImportError:
    class DDGSException(Exception):
        pass

class TestBackendApi(unittest.TestCase):

    def setUp(self):
        if not has_requirements:
            self.skipTest("gui is not installed")
        self.app = MagicMock()
        self.api = Backend_Api(self.app)

    def test_version(self):
        response = self.api.get_version()
        self.assertIn("version", response)
        self.assertIn("latest_version", response)

    def test_get_models(self):
        response = self.api.get_models()
        self.assertIsInstance(response, list)
        self.assertTrue(len(response) > 0)

    def test_get_providers(self):
        response = self.api.get_providers()
        self.assertIsInstance(response, list)
        self.assertTrue(len(response) > 0)

    def test_search(self):
        if not has_search:
            self.skipTest("import error")
            return
        try:
            result = asyncio.run(search("Hello"))
        except DDGSException as e:
            self.skipTest(e)
        except MissingRequirementsError:
            self.skipTest("search is not installed")
        self.assertGreater(len(result), 0)
